package com.sport.facilities.booking.system.htmlchecker;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import android.util.Log;

import com.sport.facilities.booking.system.core.HtmlHelper;
import com.sport.facilities.booking.system.model.Booking;

public class TransferBookingResultChecker extends BasicChecker {

	private final String TAG = "TransferBookingResultChecker";

	public List<Booking> getTransferBookingFormfromHtml(
			String transferbookingFormHtml) throws ParserException, Exception {

		super.hasError(transferbookingFormHtml);
		// Get NodeList from HTML
		List<Booking> bookingList = new ArrayList<Booking>();

		NodeFilter filter = null;
		filter = new HasAttributeFilter("type", "hidden");

		NodeList nodelist = HtmlHelper.getNodeListByTag(
				transferbookingFormHtml, "table");
		NodeList radio_nodelist = HtmlHelper.getNodeListByTag(
				transferbookingFormHtml, "form");
		TagNameFilter fontFilter = new TagNameFilter("FONT");

		HasAttributeFilter hasSizeAttribute = new HasAttributeFilter("size");
		AndFilter testFilter = new AndFilter(fontFilter, hasSizeAttribute);

		NodeList foundNodeList = nodelist.extractAllNodesThatMatch(testFilter,
				true);
		NodeList foundNodeList2 = radio_nodelist.extractAllNodesThatMatch(
				filter, true);

		String radioValue = foundNodeList2.elementAt(0).toHtml();
		radioValue = radioValue.replaceAll("\\D+", "");

		Booking booking = new Booking();
		// set radio Id
		booking.setId(radioValue);

		Node userIdNode = foundNodeList.elementAt(1).getNextSibling();
		booking.setUserId(userIdNode.getNextSibling().toHtml());
		Log.d(TAG, "Transfer Booking : " + userIdNode.getNextSibling().toHtml());

		Node centreNode = foundNodeList.elementAt(3).getNextSibling();
		booking.setCentre(centreNode.getNextSibling().toHtml());
		Log.d(TAG, "Transfer Booking : " + centreNode.getNextSibling().toHtml());

		Node courtNode = foundNodeList.elementAt(5).getNextSibling();
		booking.setCourt(courtNode.getNextSibling().toHtml());
		Log.d(TAG, "Transfer Booking : " + courtNode.getNextSibling().toHtml());

		Node activityNode = foundNodeList.elementAt(7).getNextSibling();
		booking.setActivity(activityNode.getNextSibling().toHtml());
		Log.d(TAG, "Transfer Booking : " + activityNode.getNextSibling().toHtml());

		Node dateNode = foundNodeList.elementAt(9).getNextSibling();
		booking.setDate(dateNode.getNextSibling().toHtml());
		Log.d(TAG, "Transfer Booking : " + dateNode.getNextSibling().toHtml());

		Node timeNode = foundNodeList.elementAt(11).getNextSibling();
		booking.setTime(timeNode.getNextSibling().toHtml());
		Log.d(TAG, "Transfer Booking : " + timeNode.getNextSibling().toHtml());
		
		
		bookingList.add(booking);

		return bookingList;
	}

}
